# Augeas library build notes

## Linux

## Common

Prepare the environment

```bash
export TOOLCHAIN="/usr/local/osquery-toolchain"
export PKG_CONFIG_SYSROOT_DIR=${TOOLCHAIN}
export PKG_CONFIG_PATH=
export PKG_CONFIG_LIBDIR=${TOOLCHAIN}/usr/lib/pkgconfig
export PATH="${TOOLCHAIN}/usr/bin:${PATH}"
export CFLAGS="--sysroot ${TOOLCHAIN}"
export CXXFLAGS="${CFLAGS}"
export CPPFLAGS="${CFLAGS}"
export LDFLAGS="${CFLAGS}"
export CC=clang
```

Disables tests, documentation and examples:

```bash
sed -i '/SUBDIRS += tests man doc examples/d' Makefile.am
sed -i '/SUBDIRS += gnulib\/tests/d' Makefile.am
```

Disable the command line tools:

```bash
sed -i '/AUGEAS_CHECK_READLINE/d' configure.ac
sed -i '/bin_PROGRAMS = augtool augparse augmatch/c\bin_PROGRAMS = ' src/Makefile.am
```

Pass the include headers of the libxml2 library we have in osquery (make sure to set the correct architecture!). Also set a fake library to pass the configuration step.

```bash
export LIBXML_CFLAGS="-I/home/ubuntu/osquery/libraries/cmake/source/libxml2/src/include -I/home/ubuntu/osquery/libraries/cmake/source/libxml2/generated/config/linux/x86_64 -I/home/ubuntu/osquery/libraries/cmake/source/libxml2/generated/version/linux/x86_64"
export LIBXML_LIBS="-lpthreads"
```

Now configure the library

```bash
./autogen.sh \
  --enable-static \
  --enable-shared=no \
  --without-selinux
```

Build the library

```bash
make -j $(nproc)
```

Copy the generated augeas files:

 - `config.h`
 - `datadir.h`
 - `parser.c`
 - `parser.h`
 - `lexer.c`

Copy the generated gnulib files:

 - `config.h` (use the augeas one)
 - All the files containing `GENERATED AUTOMATICALLY` under `lib`

## macOS

Ensure to have GNU `bison` and `flex` installed from brew

```bash
brew install bison flex
```

### x86_64

```bash
export PATH="/usr/local/opt/bison/bin:/usr/local/opt/flex/bin:${PATH}"
export CFLAGS="-isysroot /Applications/Xcode_13.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -mmacosx-version-min=10.14 -Wunguarded-availability-new"
export LDFLAGS="${CFLAGS}"
export CC=clang
```

Disables tests, documentation and examples:

```bash
gsed -i '/SUBDIRS += tests man doc examples/d' Makefile.am
gsed -i '/SUBDIRS += gnulib\/tests/d' Makefile.am
```

Disable the command line tools:

```bash
gsed -i '/AUGEAS_CHECK_READLINE/d' configure.ac
gsed -i '/bin_PROGRAMS = augtool augparse augmatch/c\bin_PROGRAMS = ' src/Makefile.am
```

Pass the include headers of the libxml2 library we have in osquery (make sure to set the correct architecture!). Also set a fake library to pass the configuration step.

```bash
export LIBXML_CFLAGS="-I/Users/alessandro/Projects/osquery/libraries/cmake/source/libxml2/src/include -I/Users/alessandro/Projects/osquery/libraries/cmake/source/libxml2/generated/config/macos/x86_64 -I/Users/alessandro/Projects/osquery/libraries/cmake/source/libxml2/generated/version/macos/x86_64"
export LIBXML_LIBS="-lpthreads"
```

Now configure the library

```bash
./autogen.sh \
  --enable-static \
  --enable-shared=no \
  --without-selinux
```

Build the library

```bash
make -j $(nproc)
```

Copy the generated augeas files:

 - `config.h`
 - `datadir.h`
 - `parser.c`
 - `parser.h`
 - `lexer.c`

Copy the generated gnulib files:

 - `config.h` (use the augeas one)
 - All the files containing `AUTOGENERATED` under `lib`

### macOS ARM (M1, M2 etc.)

Same as above, but use the following environment variables instead:

```bash
export CFLAGS="-isysroot /Applications/Xcode_13.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -mmacosx-version-min=10.15 -Wunguarded-availability-new -target arm64-apple-macos10.15"
export LDFLAGS="${CFLAGS}"
export CC=clang
```

Also pass this additional flag to `autogen.sh`: `--host=arm64-apple-macos10.15`
